package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static Session session = null;
private static SessionFactory sessionFactory = null;
	
	private static SessionFactory getSessionFactory(){
		if (sessionFactory == null) {
			AnnotationConfiguration cfg = new AnnotationConfiguration();
			Configuration config = cfg.configure("hibernate.cfg.xml");
			sessionFactory = config.buildSessionFactory();
		}
		return sessionFactory;
	}
	
	public static  Session getSession(){
		if(session == null || !session.isOpen()){
			session = getSessionFactory().openSession();
		}
		return session;
	}

}